//
//  StatusCell.swift
//  GZWeibo
//
//  Created by Apple on 15/9/25.
//  Copyright © 2015年 itcast. All rights reserved.
//

import UIKit
import SnapKit

/// 图像宽度
let WBStatusCellIconWidth: CGFloat = 35
/// 控件之间的间距
let WBStatusCellMargin: CGFloat = 12

/// 微博 Cell
class StatusCell: UITableViewCell {

    /// 微博数据视图模型
    var viewModel: StatusViewModel? {
        didSet {
            topView.viewModel = viewModel
            contentLabel.text = viewModel?.status.text
            
            // 设置配图视图数据
            pictureView.viewModel = viewModel
            
            // 修改配图视图高度
            pictureView.snp_updateConstraints { (make) -> Void in
                // 使用计算完成的大小，设置约束数值！
                make.width.equalTo(pictureView.bounds.width)
                make.height.equalTo(pictureView.bounds.height)
                
                let offset = (pictureView.bounds.height == 0) ? 0 : WBStatusCellMargin
                make.top.equalTo(contentLabel.snp_bottom).offset(offset)
            }
        }
    }
    
    /// 根据视图模型计算行高
    ///
    /// - parameter vm: 视图模型
    ///
    /// - returns: 行高
    func rowHeight(vm: StatusViewModel) -> CGFloat {
        // 1. 设置视图模型
        viewModel = vm
        
        // 2. 更新约束
        layoutIfNeeded()
        
        // 3. 返回底部视图的最大 Y 值
        return CGRectGetMaxY(bottomView.frame)
    }
    
    // MARK: - 构造函数
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /// 设置界面
    private func setupUI() {
        // 1. 设置背景颜色
        backgroundColor = UIColor.whiteColor()
        
        // 2. 添加控件
        contentView.addSubview(topView)
        contentView.addSubview(contentLabel)
        contentView.addSubview(pictureView)
        contentView.addSubview(bottomView)
        
        // 3. 自动布局
        topView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentView.snp_top)
            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
            make.height.equalTo(2 * WBStatusCellMargin + WBStatusCellIconWidth)
        }
        
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(topView.snp_bottom).offset(WBStatusCellMargin)
            make.left.equalTo(contentView.snp_left).offset(WBStatusCellMargin)
        }
        
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentLabel.snp_bottom).offset(WBStatusCellMargin)
            make.left.equalTo(contentLabel.snp_left)
            make.width.equalTo(290)
            make.height.equalTo(290)
        }
        
        bottomView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(pictureView.snp_bottom).offset(WBStatusCellMargin)
            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
            make.height.equalTo(44)
            
            // 设定底部约束
            // make.bottom.equalTo(contentView.snp_bottom)
        }
    }
    
    // MARK: - 懒加载控件
    /// 顶部视图
    private lazy var topView: StatusCellTopView = StatusCellTopView()
    /// 内容标签
    private lazy var contentLabel: UILabel = UILabel(title: "微博正文",
        color: UIColor.darkGrayColor(),
        fontSize: 15,
        layoutWidth: UIScreen.mainScreen().bounds.width - 2 * WBStatusCellMargin)
    /// 配图视图
    private lazy var pictureView: StatusCellPictureView = StatusCellPictureView()
    /// 底部视图
    private lazy var bottomView: StatusCellBottomView = StatusCellBottomView()
}
